package com.selenium.core.functionlibrary;

/**
 * Provides methods to collect dynamic objects from web page
 * 
 * @author phebbar
 *
 */
public class WebObjects extends SafeSelenium {
	
	
	/**
	 * Provides object ids or name for HTML object input
	 * 
	 * @param inputType Could be checkbox, TextBox or Radio button
	 * @return	objectId 
	 * 
	 */
	public static String[] getInputObjects(String inputType) {
		
		StringBuilder sb = new StringBuilder("var objectId = [];")
		.append("var cnt = 0;")
		.append("var objectFields = [];")
		.append("objectFields  = window.document.getElementsByTagName('input');")
		.append("for(var i=0; i<objectFields.length; i++) {")
		.append("if(objectFields[i].getAttribute('type') == '")
		.append(inputType).append("'){")						
		.append("objectId[cnt] = objectFields[i].id || objectFields[i].name;")
		.append("cnt += 1;}}objectId.toString();");
		
		String script = selenium.getEval(sb.toString()).trim();		
		String[] objectId = script.split(","); // Split the string.
		
		return objectId;		
	}
	
	/**
	 * Provides object id or name of HTML objects 
	 * 
	 * @param objectType Could be text, textarea etc
	 * @return objectId
	 */
	public static String[] getCommonObjects(String objectType) {
		
		StringBuilder sb = new StringBuilder("var objectId = [];")		
		.append("var cnt = 0;")
		.append("var objectFields = [];")
		.append("objectFields = window.document.getElementsByTagName('")
		.append(objectType).append("');")
		.append("for(var i=0; i<objectFields.length; i++) {")
		.append("objectId[cnt] = objectFields[i].name || objectFields[i].id;")
		.append("cnt += 1;}objectId.toString();");// Convert array in to string.
	
		String script = selenium.getEval(sb.toString()).trim();		
		String[] objectId = script.split(","); // Split the string.
		
		return objectId;
	}

	/**
	 * Retrieves all dropdown objects in a web page
	 * @return dropDownObjects
	 */
	public static String[] getAllDropDowns () {
		String[] dropDownObjects = getCommonObjects("select");
		return dropDownObjects; 
	}
	
	/**
	 * Retrieves all multiline text box objects in a web page
	 * @return textareaObjects
	 */
	public static String[] getAllMultiLineTextbox() {
		String[] textareaObjects = getCommonObjects("textarea");
		return textareaObjects; 				
	} 

	/**
	 * Retrieves all radio button objects in a web page
	 * @return radioButtonObjects
	 */
	public static String[] getAllRadioButtons () {
		String[] radioButtonObjects = getInputObjects("radio");
		return radioButtonObjects;		
	}

	/**
	 * Retrieves all check box objects in a web page
	 * @return checkBoxObjects
	 */

	public static String[] getAllCheckBoxes () { 
		String[] checkBoxObjects = getInputObjects("checkbox");
		return checkBoxObjects;
	}

	/**
	 * Retrieves all text box objects in a web page
	 * @return textBoxObjects
	 */
	public static String[] getAllTextboxes () {
		String[] textBoxObjects = getInputObjects("text");
		return textBoxObjects;		
	} 
}
